home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 909 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  63 lines

  1. Newsgroups: comp.lang.c
  2. Path: gail.ripco.com!mambuhl
  3. From: mambuhl@ripco.com (Martin Ambuhl)
  4. Subject: Re: warning: possibly inc
  5. X-Nntp-Posting-Host: golden.ripco.com
  6. Message-ID: <DKy500.2q2@rci.ripco.com>
  7. Sender: usenet@rci.ripco.com (Net News Admin)
  8. Organization: Ripco Internet BBS Chicago
  9. Date: Wed, 10 Jan 1996 03:43:11 GMT
  10. X-Ident-Sender: mambuhl
  11.  
  12. Bill Simpson <wsimpson@uwinnipeg.ca>
  13. in <Pine.OSF.3.91.960109091920.6447A-100000@io.UWinnipeg.ca> asks:
  14.  
  15. >I get the above warning
  16. [warning: possibly incorrect assignment]
  17. >when I compile code using the following
  18. >function.  It flags the **** line.
  19.  
  20. >****        while(fp=fopen(file_name,"r"))
  21. use:
  22.              while((fp=fopen(file_name,"r")))
  23.  
  24. >How can I write this code so the compiler does not issue this warning?
  25. >Please don't suggest I just tell the compiler to shut up.  In general
  26. >the warnings are useful.  I do not want to just ignore the warning either.
  27. >I have the FAQ and haven't seen this discussed.
  28.  
  29. While you are absolutely right to not ignore warnings, there is a
  30. difference between turning off warnings and doing what is necessary in
  31. the code to suppress them.
  32.  
  33. In this case, you are using an assignment where it is common to have
  34. have a comparison, e.g.:
  35.              while(fp==fopen(file_name,"r"))
  36. This warning tells you that you may not be doing what you intend.  The
  37. normal way to tell the compiler that you know what you are doing is to
  38. enclose the assignment in ()s so the compiler see a while statement like
  39.             while(result_of_assignment)
  40.  
  41. You could add an explicit comparison to the suggested change
  42.              while((fp=fopen(file_name,"r")) != NULL)
  43. but this is a style issue only.  The two forms are functionally the
  44. same. For example, my compiler produces exactly the same code (below)
  45. for each:
  46.  
  47. L5:
  48.         pushl $LC1
  49.         pushl _file_name
  50.         call _fopen
  51.         movl %eax,_fp
  52.         addl $8,%esp
  53.         testl %eax,%eax
  54.         je L6
  55. ; while block goes here
  56.         jmp L5
  57.         .align 2,0x90
  58. L6:
  59.             
  60. --
  61. * Martin Ambuhl       net: mambuhl@ripco.com
  62. * Chicago, IL (USA)    
  63.